<template>
  <div class="air">
    <el-row :gutter="20">
        <el-col :span="24" class="paddCol" >
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                  <span :style="this.alarmstate == 0 ? '':'color:#ea2e2e'"><i class="el-icon-menu"></i> <span class="shebeiname">{{this.data.name}}</span></span>
              </div>
              <div class="temp_card">
                <div class="xtg402_data">
                  <div class="irf_allk">
                    <div class="light_topdiv">
                      <div  class="light_center_light">
                        <span>当前光强: </span>
                        <span class="light_light">{{this.data.unit[31].value.toFixed(0)}}</span>
                        <span class="light_flag"> LUX</span>
                      </div>
                      <div class="light_center_border">
                        <div class="light_control" @click="switchOpen(5)">
                          <img src="../../assets/light1.png" alt="" v-if="this.dataShow[5].value == 1">
                          <p v-if="this.dataShow[5].value == 1">照明</p>
                          <img src="../../assets/light1_gray.png" alt="" v-if="this.dataShow[5].value == 0">
                          <p class="gray" v-if="this.dataShow[5].value == 0">照明</p>
                        </div>
                        <div class="light_control" @click="switchOpen(6)">
                          <img src="../../assets/light1.png" alt="" v-if="this.dataShow[6].value == 1">
                          <p v-if="this.dataShow[6].value == 1">照明</p>
                          <img src="../../assets/light1_gray.png" alt="" v-if="this.dataShow[6].value == 0">
                          <p class="gray" v-if="this.dataShow[6].value == 0">照明</p>
                        </div>
                        <div class="light_control" @click="switchOpen(7)">
                          <img src="../../assets/light1.png" alt="" v-if="this.dataShow[7].value == 1">
                          <p v-if="this.dataShow[7].value == 1">照明</p>
                          <img src="../../assets/light1_gray.png" alt="" v-if="this.dataShow[7].value == 0">
                          <p class="gray" v-if="this.dataShow[7].value == 0">照明</p>
                        </div>
                        <div :class="this.dataShow[0].value == 0 ? 'light_control light_control2':'light_control'">
                          <div v-if="this.dataShow[0].value == 1" @click="switchOpen(8)">
                            <img src="../../assets/light1.png" alt="" v-if="this.dataShow[8].value == 1">
                            <p v-if="this.dataShow[8].value == 1">照明</p>
                            <img src="../../assets/light1_gray.png" alt="" v-if="this.dataShow[8].value == 0">
                            <p class="gray" v-if="this.dataShow[8].value == 0">照明</p>
                          </div>
                          <div v-if="this.dataShow[0].value == 0" @click="switchOpen(9)">
                            <img src="../../assets/light2.png" alt="" v-if="this.dataShow[9].value == 1">
                            <p v-if="this.dataShow[9].value == 1">开门</p>
                            <img src="../../assets/light2_gray.png" alt="" v-if="this.dataShow[9].value == 0">
                            <p class="gray" v-if="this.dataShow[9].value == 0">开门</p>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="temp_time_mode">
                    <div class="temp_peak_name">
                      <i class="el-icon-time"></i>
                      时间统计
                    </div>
                    <div class="temp_time">
                      <div class="temp_time_line">
                        <p class="temp_warning_title">首次上线：</p>
                        <p class="temp_time_year">{{ this.firstconnecttimeYear }} </p> 
                        <p class="temp_time_hour">{{ this.firstconnecttimeHour}}</p>
                      </div>
                      <div class="temp_time_line" v-if="this.connecttime > 0">
                        <p class="temp_warning_title">最近上线：</p>
                        <p class="temp_time_year">{{ this.connecttimeYear }} </p>
                        <p class="temp_time_hour">{{ this.connecttimeHour }}</p>
                      </div>
                      <div class="temp_time_line" v-if="this.devtimeshow == 1">
                        <p class="temp_warning_title">累计时长：</p>
                        <p class="temp_time_h"> {{this.totaltime}} </p> <span class="temp_time_s"> h</span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </el-card>
        </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  props: ["msg"],
  data() {
    return {
      alarmstate:0,//在线离线
      timeGetData:'',//接口方法的定时器
      data: "", //当前设备数据  真实数据
      dataShow: "", //当前设备数据  展示用数据
      dataor:1,//判断接口是否成功，1表示有返回成功，0表示失败，3之内无返回就置0表示失败
      clickGetData:'',//延迟保存计时器
      clickNumber:0,//延迟保存点击次
      //属性数据
      firstconnecttimeYear:'',//首次上线年月日
      firstconnecttimeHour:'',//首次上线时分
      connecttime:0,//最近上线时间戳
      connecttimeYear:'',//最近上线年月日
      connecttimeHour:'',//最近上线时分
      totaltime:0,//累计运行时间
      devtimeshow:localStorage.getItem('累计时长'),//累计运行时间显示隐藏
      localname:'',//本地储存名称
    };
  },
  mounted() {
  },
  created() {
    // 判断是否是直接访问当前设备页面
    if(localStorage.getItem('当前访问路径') == 'overview2'){
      this.localname = "6.0展示设备卡片"
    }else{
      this.localname = "当前设备编号"
    }
    this.info = JSON.parse(localStorage.getItem(this.localname))
    this.dataShow = JSON.parse(localStorage.getItem("menuDataJson")).unit
    this.data = JSON.parse(localStorage.getItem("menuDataJson"))
    this.getData(JSON.parse(localStorage.getItem(this.localname)))
    this.getDataStart()
  },
  methods: {
    //重新定时走接口
    getDataStart(){
      this.$nextTick(() => {
          this.timeGetData = setInterval(() => {
            if(this.dataor == 0){ //接口失败提示
              this.$message({
                message: '服务器访问失败',
                type: 'error',
                duration: 1000
              });
            }
            this.getData(JSON.parse(localStorage.getItem(this.localname)))
            this.dataor = 0
          }, 3000); 
      })
    },
    getData(info){
      this.info = info
      if(info.uid == null || info.uid == "null"){//清空缓存第一次进入时找不到info.uid，页面为空页面的处理
        info.uid = 0
      }
      var params = {
          "getdevinfo":[
            {
              "uid": info.uid,
              "type": info.type,
              "addr": info.addr,
              "unittype":"id-name-ready-value-flag-max-min"
            }
          ],
          [localStorage.getItem("cookieName")]:localStorage.getItem("_token")
      }
      this.$http.post(this.$baseUrl, params).then((res) => {
        this.dataor = 1// 接口成功置1
        if(res.data.getdevinfo == undefined){ //数据返回不全失败提示
          this.$message({
            message: '服务器访问失败',
            type: 'error',
            duration: 1000
          });
        }else{
          if(this.clickNumber == 0){ // 延迟判断状态中不接受真实状态
            var st = []
            for(var a = 0; a < res.data.getdevinfo[0].unit.length; a++){
              st.push(
                {
                  id:res.data.getdevinfo[0].unit[a].id, 
                  value:res.data.getdevinfo[0].unit[a].value
                }
              )
            }
            this.dataShow = st;//展示状态
          }
          this.data = res.data.getdevinfo[0];
          this.alarmstate = res.data.getdevinfo[0].alarmstate
          // 上次上线时间
          this.firstconnecttimeYear = this.formatDate(this.data.firstconnecttime*1000, 'YYYY-MM-DD')
          this.firstconnecttimeHour = this.formatDate(this.data.firstconnecttime*1000, 'H:m')
          // 最近上线时间
          this.connecttime = this.data.connecttime
          this.connecttimeYear = this.formatDate(this.data.connecttime*1000, 'YYYY-MM-DD')
          this.connecttimeHour = this.formatDate(this.data.connecttime*1000, 'H:m')
          //累计运行时间 拿到的分钟转成小时
          this.devtimeshow = localStorage.getItem('累计时长')
          this.totaltime = (this.data.totaltime/60).toFixed(0)
        }
      })
    },
    //控制方法
    switchOpen(id){
      var value = 0;
      if(this.dataShow[id].value == 0){
        value = 1
      }else{
        value = 0
      }
      // 延迟保存方法
      clearInterval(this.clickGetData);
      this.clickGetData = null;
      this.clickNumber = 1;  //1秒后走逻辑，所以计时从1开始
      this.dataShow[id].value = value //修改展示状态值
      console.log('下发'+value)
      this.clickGetData = setInterval(() => { 
        this.clickNumber++;
        if(this.data.unit[id].value != this.dataShow[id].value){ // 返回的值和下发值不一致，等待返回
          console.log('错误等待'+this.clickNumber+'秒')
        }else{ // 返回的值和下发值一致，取消等待，拿取真实值
          console.log('正确')
          clearInterval(this.clickGetData);
          this.clickGetData = null;
          this.clickNumber = 0
        }
        if(this.clickNumber > this.$store.state.waittTime){ //等待5秒后，取消等待，拿取真实值 waittTime统一宏变量  计时从1开始，所以等待时间要再+1
          console.log('超时')
          clearInterval(this.clickGetData);
          this.clickGetData = null;
          this.clickNumber = 0
        }
      }, 1000); 
      var params = {
          "setdevinfo":[
              {
                  "uid": this.info.uid,
                  "type": this.info.type,
                  "addr": this.info.addr,
                  "unit":[
                    {
                      id: id,
                      value: value
                    }
                  ]
              }
          ],
          [localStorage.getItem("cookieName")]:localStorage.getItem("_token")
      }
      this.$http.post(this.$baseUrl, params).then((res) => {
          if(res.data.result == 'ok'){
              
          }else{
              this.$notify({
                  title: '操作失败',
                  type: 'warning',
                  duration: 1500
              });
          }
      });
    },
  },
  // 清除定时器
    beforeDestroy() {
        clearInterval(this.timeGetData);
        this.timeGetData = null;
        // 下发控制计时器
        clearInterval(this.clickGetData);
        this.clickGetData = null;
    },
};
</script>

<style scoped>
.el-card {
  background: none;
  color: #fff;
  border: 1px solid rgba(89, 129, 165, 0.4);
}
/deep/ .el-card__header {
  background: rgba(89, 129, 165, 0.2);
  padding: 16px 20px;
  border-bottom: none;
}
.box-card {
  /* overflow: hidden; */
}
.temp_card{
  height: 850px;
}
.clearfix{
  font-size: 18px;
}
.shebeiname{
  font-weight: bold;
}
.xtg402_data{
  display: flex;
}
.irf_allk{
  width: 1000px;
  height: 540px;
  margin-top: 90px;
  /* background: #0000002b; */
}
/* 内容 */
/* 内容框 */
.light_topdiv{
  height: 540px;
  width: 100%;
  /* border: 1px solid #5981a566; */
  border: 1px solid #5981a566;
  box-shadow: 0px 0px 12px #b4e9ff73;
  border-radius: 10px;
}
.light_center_light{
  width: 100%;
  /* text-align: center; */
  margin-top: 100px;
  padding-left: 85px;
}
.light_light{
  font-size: 24px;
  color: #5bc9fc;
}
.light_flag{
  color: #b7b7b7;
  font-size: 14px;
}
.light_center_border{
  width: 90%;
  height: 200px;
  display: flex;
  flex: 1;
  /* border: 1px solid #ffffff69; */
  margin: 0 auto;
  margin-top: 50px;
  justify-content: space-around;
}
.light_control{
  width: 22%;
  height: 100%;
  border: 1px solid #ffffff26;
  box-shadow: 4px 4px 10px 1px #93e3f74a;
  border-radius: 10px;
  text-align: center;
  padding-top: 60px;
  cursor: pointer;
}
.light_control img{
  width: 40px;
}
.light_control2 img{
  width: 52px;
}
.light_control p{
  font-size: 16px;
  margin-top: 5px;
  color: #5bc9fc;
}
.gray{
  color: #7b7a7a !important;
}
/* 在线时间 */
.temp_time_mode{
  width: 400px;
  height: 540px;
  /* margin: 0 auto; */
  padding-top: 30px;
  border-radius: 10px;
  background: #0000002b;
  margin-bottom: 20px;
  margin-top: 90px;
  margin-left: 50px;
}
.temp_peak_name{
  width: 100%;
  font-size: 22px;
  margin-left: 20px;
}
.temp_time{
  margin-top: 80px;
  margin-left: 25px;
  height: 310px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}
.temp_time_line{
  margin-bottom: 90px;
  position: relative;
  top: 11px;
  display: flex;
  font-size: 20px;
}
.temp_time{
  margin-right: 30px;
  color: #d4d3d3;
}
.temp_time .temp_warning_title{
  width: 110px;
}
.temp_time_year{
  width: 150px;
  font-family: electronicFont;
  font-size: 28px;
  color: #5eceff;
}
.temp_time_hour{
  width: 40px;
  font-family: electronicFont;
  font-size: 28px;
  color: #5eceff;
}
.temp_time_h{
  font-family: electronicFont;
  font-size: 35px;
  color: #5eceff;
  position: relative;
  top: -5px;
}
.temp_time_s{
  color: #5eceff;
  margin-left: 5px;
}
</style>